// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        buttonPrefab: {
            default: null,        // The default value will be used only when the component attaching
                                  // to a node for the first time
            type: cc.Prefab, // optional, default is typeof default
        },

        buttonSound: {
            default: null,
            type: cc.AudioClip,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.m_buttonNode = cc.find('Canvas/m_buttonNode')

        var bigSize = new cc.Size(120, 120)
        var cols = 4

        for(var i=0; i<=ck.userData.maxLevel; ++i)
        {
            var button = cc.instantiate(this.buttonPrefab);
            button.getComponent('ChooseButton').init(i, i>ck.userData.canPlayLevel, this.onClickLevel.bind(this));
            button.parent = this.m_buttonNode;
            button.x = Math.floor(i%cols)*bigSize.width;
            button.y = -Math.floor(i/cols)*bigSize.height;
            ck.log('button', i, button.x, button.y)
        }
    },

    onClickLevel(level){
        cc.audioEngine.play(this.buttonSound);
        cc.director.loadScene("GameScene", this.onLoadGameScene.bind(this, level))
    },

    onLoadGameScene(level){
        ck.getGameScene().startLevel(level)
    },
});

